package com.gyh.stlserver.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyh.stlserver.entity.Course;
import com.gyh.stlserver.entity.Member;
import com.gyh.stlserver.entity.Msg;
import com.gyh.stlserver.entity.Reserve;
import com.gyh.stlserver.mapper.*;
import com.gyh.stlserver.service.CourseService;
import com.gyh.stlserver.service.ReserveService;
import com.gyh.stlserver.vo.CourseVo;
import com.gyh.stlserver.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ReserveMapper reserveMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Resource
    private MsgMapper msgMapper;
    @Resource
    private RoomMapper roomMapper;
    public PageInfo<CourseVo> getAllByCourseVo(Integer pageNum,Integer pageSize,Course course){
        PageHelper.startPage(pageNum,pageSize);
        List<CourseVo> courseVos = courseMapper.select(course);
        for (CourseVo courseVo : courseVos) {
            /*根据课程id查询会员id*/
            List<Integer> mids = reserveMapper.selectMidByCid(courseVo.getId());
            /*根据会员id查询会员*/
            List<MemberVo> members = memberMapper.selectMemberByIds(mids);
            if (ObjectUtil.isNotEmpty(members)){
                for (MemberVo member : members) {
                    Reserve reserve = reserveMapper.selectReserveByMidAndCid(new Reserve()
                            .setCourseId(courseVo.getId())
                            .setMemberId(member.getId()));
                    member.setStatus(reserve.getStatus());
                }
            }
            courseVo.setChildren(members);
        }
        return new PageInfo<>(courseVos);
    }

    @Override
    @Transactional
    public int addCourse(Course course) {
        /*添加课程之前需要判断当课程所在房间的开课时间是否与其他课程冲突*/
        Date endTime = new Date(course.getTime().getTime()+course.getLength()*60*1000);
        /*如果有已有课程的结束时间小于加入课程的开始时间*/
        Course course1 = courseMapper.selectCourseByRoomIdAndTime(course.getRoomId(),course.getTime(),endTime);
        if (ObjectUtil.isNotEmpty(course1)){
            return -1;
        }
        /*教练是否有时间*/
        Course course2 = courseMapper.selectCourseByCidAndTime(course.getCoachId(),course.getTime(),endTime);
        if (ObjectUtil.isNotEmpty(course2)){
            return -2;
        }
        int i = courseMapper.insert(course);
        //给每个会员分发课程信息
        List<Member> members = memberMapper.select(null);
        Msg msg = new Msg();
        msg.setCourseId(course.getId());
        msg.setTime(new Date());
        msg.setStatus(1);
        for (Member member : members) {
            msg.setMemberId(member.getId());
            msgMapper.insert(msg);
        }
        return i;
    }

    @Override
    public int editCourse(Course course) {
        /*修改课程之前需要判断当课程所在房间的开课时间是否与其他课程冲突*/
        Date endTime = new Date(course.getTime().getTime()+course.getLength()*60*1000);
        Course course1 = courseMapper.selectCourseByRoomIdAndTime(course.getRoomId(),course.getTime(),endTime);
        /*不等于自身，并且不为空*/
        if (ObjectUtil.isNotEmpty(course1)&&!course1.getId().equals(course.getId())){
            return -1;
        }
        /*教练是否有时间*/
        Course course2 = courseMapper.selectCourseByCidAndTime(course.getCoachId(),course.getTime(),endTime);
        if (ObjectUtil.isNotEmpty(course2)&&!course.getId().equals(course2.getId())){
            return -2;
        }
        return courseMapper.update(course);
    }

    @Override
    public int remove(Integer id) {
        return courseMapper.delete(id);
    }

    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.selectCourseById(id);
    }

    @Override
    public List<CourseVo> getAllCourseVo() {
        return courseMapper.select(null);
    }

    @Override
    public List<Course> getAllCourse() {
        return courseMapper.selectAll();
    }
}
